package SIM_settings;
require Exporter;

use strict;


# Settings module for shared perl settings.
our @ISA        = ("Exporter");
our @EXPORT     = qw(
                     $SIM_MonDevices $SIM_CiscoPwds $SIM_TftpBootDir
		     $SIM_beep $SIM_History $manDomain
		     $httpScriptName $remoteUser $userLevel $ipDatabaseDir $macDatabaseDir $simPtrDir $sharedConfDir $graphDir
                     $debugging
		     $switchInfoDir $deviceDir $oidHelpDir $sipmanStarter
		     $snmpStrings $inaddrSrvList $dnsCache $baseDomain $confRrdTool $searchIndexFile
                     %errorColor %errorAllert %SIMparam %nodeColor %nodePollType %nodeDnsType %nodeManType %nodeDescription
                    );
our @VERSION    = 0.01;                 # version number


# exported environment settings.
our $httpScriptName = $ENV{'SCRIPT_NAME'} || die "Oops no env found for SCRIPT_NAME.";
our $remoteUser = $ENV{'REMOTE_USER'} || die "Oops no env found for REMOTE_USER.";
our $userLevel = $ENV{'USERLEVEL'};
our $baseDomain = $ENV{'SIM_BASEDOMAIN'} || die "Oops no env found for SIM_BASEDOMAIN.";
our $confRrdTool = $ENV{'SIM_CONFRRDTOOL'} || die "Oops no env found for SIM_CONFRRDTOOL.";
our $graphDir = $ENV{'SIM_GRAPHTMPDIR'} || die "Oops no env found for SIM_GRAPHTMPDIR.";
our $switchInfoDir = "$ENV{'SIM_SWITCHINFO'}" || die "Oops no env found for SIM_SWITCHINFO.";
our $deviceDir = $ENV{'SIM_DEVICED'} || die "Oops no env found for SIM_DEVICED.";
our $searchIndexFile = $ENV{'SIM_SEARCHINDEX'} || die "Oops no env found for SIM_SEARCHINDEX.";
our $oidHelpDir = $ENV{'SIM_OIDHELPD'} || die "Oops no env found for SIM_OIDHELPD.";
our $sipmanStarter = $ENV{'SIM_STARTER'} || die "Oops no env found for SIM_STARTER.";
our $manDomain = $ENV{'SIM_MANDOMAIN'} || die "Oops no env found for SIM_MANDOMAIN.";
our %SIMparam = ();
$SIMparam{'SIMmanDomain'} = $ENV{'SIM_MANDOMAIN'} || die "Oops no env found for SIM_MANDOMAIN.";
$SIMparam{'SIMbaseDir'} = $ENV{'SIM_BASEDIR'} || die "Oops no env found for SIM_BASEDIR.";
$SIMparam{'SIMmainDir'} = "$SIMparam{'SIMbaseDir'}/main";
$SIMparam{'SIMmainConfigDir'} = "$SIMparam{'SIMmainDir'}/configs";
$SIMparam{'SIMsharedDir'} = "$SIMparam{'SIMbaseDir'}/shared";
$SIMparam{'SIMsharedConfDir'} = "$SIMparam{'SIMsharedDir'}/configs";
$SIMparam{'SIMdataDir'} = "$SIMparam{'SIMbaseDir'}/data/$SIMparam{'SIMmanDomain'}";
$SIMparam{'SIMdataConfDir'} = "$SIMparam{'SIMdataDir'}/configs";
$SIMparam{'SIMsystemDnsServers'} = "system"; # default setting dns servers

# set default dns servers

if ( -r "/etc/resolv.conf" ) {
  open ( RESO, "/etc/resolv.conf");
  my @lines = <RESO>;
  close RESO;
  foreach my $line (@lines) {
    my ( $type, $info ) = split(' ', $line);
    if ( $type eq "nameserver" ) {
      if ( $SIMparam{'SIMsystemDnsServers'} eq "system" ) {
        $SIMparam{'SIMsystemDnsServers'} = $info;
      } else {
        $SIMparam{'SIMsystemDnsServers'} = "$SIMparam{'SIMsystemDnsServers'} $info";
      }
    }
  }
} else {
  # if nothing is defined in resolv.conf, set it to 127.0.0.1.
  $SIMparam{'SIMsystemDnsServers'} = "127.0.0.1";
}



my @fileList = ();
push @fileList, "$SIMparam{'SIMmainConfigDir'}/settings2.conf\n";
if ( -f "$SIMparam{'SIMsharedConfDir'}/settings2.conf" ) {
  push @fileList, "$SIMparam{'SIMsharedConfDir'}/settings2.conf\n";
}
if ( -f "$SIMparam{'SIMdataConfDir'}/settings2.conf" ) {
  # dont try to read this file if no management domain is set.
  push @fileList, "$SIMparam{'SIMdataConfDir'}/settings2.conf\n";
}
foreach my $entry (@fileList) {
  chomp($entry);
  open( CONF, $entry ) || print"Oops configfile $entry not found.";
  my @fileContent = <CONF>;
  foreach my $line (@fileContent) {
    chomp($line);
    my ( $varName, $varType, $varData ) = split(' ', $line, 3);
    if ( "$line" eq "" ) {
      my $dummy;
    } elsif ( "$varName" eq "" ) {
      my $dummy;
    } elsif ( substr($varName,0,1) eq "#" ) {
      my $dummy;
    } elsif ( "$varType" eq "path" ) {
      my ( $basePath, $rest ) = split(' ', $varData);
      $SIMparam{$varName} = "$SIMparam{$basePath}/$rest";
#     print(" $varName=$SIMparam{$varName} ");
    } elsif ( "$varType" eq "var" ) {
      $SIMparam{$varName} = $varData;
    }
  }
}

our $settingsDir = $SIMparam{'SIMdataConfDir'};
our $ipDatabaseDir = $SIMparam{'SIMipDatabase'};
our $simPtrDir = $SIMparam{'SIMptrDir'};
our $macDatabaseDir = $SIMparam{'SIMmacDatab'};
our $sharedConfDir = $SIMparam{'SIMsharedConfDir'};
our $snmpStrings = $SIMparam{'SIMsnmpStrings'};
our $inaddrSrvList = $SIMparam{'SIMinaddrSrvList'};
our $dnsCache = $SIMparam{'SIMnamedCache'};
our $debugging;
if ( -f "$SIMparam{'SIMsettingsDir'}/simShowDebug-$remoteUser" ) {
  $debugging = "yes";
} else {
  $debugging = "$SIMparam{'SIMsettingsDir'}/simShowDebug-$remoteUser not found";
}

# errorInfo
my $errInfoFile = "$SIMparam{'SIMerrorTypes'}";
our %errorColor = ();
our %errorAllert = ();
if ( -r $errInfoFile ) {
  open( ERRINFO, $errInfoFile );
  my @lines = <ERRINFO>;
  foreach my $entry (@lines) {
    chomp $entry;
    my ( $test, $type, $color, $allert ) = split(' ', $entry);
    if ( "$test" eq "error" ) {
      $errorColor{$type} = $color;
      $errorAllert{$type} = $allert;
    }
  }
} else {
  print("Oops, could not read errInfoFile: $SIMparam{'SIMerrorTypes'}.");
}

our %nodeColor = ();
our %nodePollType = ();
our %nodeDnsType = ();
our %nodeManType = ();
our %nodeDescription = ();
open(INFO, $SIMparam{'SIMnodeTypes'}) || die "Cannot open $SIMparam{'SIMnodeTypes'}";
my @lines = <INFO>;
close(INFO);
foreach my $regel (@lines) {
  chomp $regel;
  my ( $prefix, $type, $color, $critical, $pollType, $dnsType, $description ) = split(' ', $regel, 7);
  if ( "$regel" eq "" ) {
    my $dummy = "";
  } elsif ( "$prefix" eq "nodeType" ) {
    $nodeColor{$type} = $color;
    $nodePollType{$type} = $pollType;
    $nodeDnsType{$type} = $dnsType;
    $nodeManType{$type} = $critical;
    $nodeDescription{$type} = $description;
  }
}



# Various references to external filenames.
# needs to be integrated in the SIMparam stuff.
our $SIM_TftpBootDir = "$settingsDir/tftpBootDir";

our $SIM_MonDevices = "$settingsDir/mon-devicelist";
our $SIM_CiscoPwds = "$settingsDir/runCiscoTelnetPwds";
our $SIM_History = "$settingsDir/testCmdHistory";
our $SIM_beep = "/sipman3/sounds/BEEP1B.WAV";


if ( -f "$SIMparam{'SIMsettingsDir'}/lessColors-$remoteUser" ) {
  $SIMparam{'SIMdisplaylessColors'} = "yes";
}

# cron jobs (set, depending whether an specific version is set, or the general version needs to be run).
if ( -f "$SIMparam{'SIMsharedConfDir'}/cronJobs" ) {
  $SIMparam{'SIMcronJobs'} = "$SIMparam{'SIMsharedConfDir'}/cronJobs";
} else {
  $SIMparam{'SIMcronJobs'} = "$SIMparam{'SIMmainConfigDir'}/cronJobs";
}

# end.
1;

